package lang4j.generator;

import org.apache.commons.lang.StringUtils;
import lang4j.parser.generated.Attribute;

/**
 * User: felix
 * Date: 28.10.2005
 * Time: 15:58:58
 */
public class JavaAttribute {
    boolean deep;
    String type;
    private final boolean optional;
    private final Attribute attribute;
    String name;

    public JavaAttribute(final String name, final String type, final boolean deep, final boolean optional, Attribute production) {
        this.deep = deep;
        this.name = name;
        if (Character.isUpperCase(name.charAt(0)))
            throw new RuntimeException("Invalid attribute name: '"+name+"'. Attributes names have to start with a lower case letter.");
        this.type = type;
        this.optional = optional;
        this.attribute = production;
    }

    public boolean isOptional() {
        return optional;
    }

    public boolean isPrimitive(){
        return type.equals("boolean")||type.equals("int")||type.equals("char")||type.equals("double");
    }

    public boolean isDeep() {
        return deep;
    }

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }

    public boolean equals(final Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        final JavaAttribute that = (JavaAttribute) o;

        if (!name.equals(that.name)) return false;
        if (!type.equals(that.type)) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = type.hashCode();
        result = 29 * result + name.hashCode();
        return result;
    }

    public String toString() {
        return type+" "+name;
    }

    public String getCapName(){
        return StringUtils.capitalize(name);
    }

    public boolean hasProduction(){
        return attribute !=null;
    }

    public Attribute getAttribute() {
        if (attribute ==null)
            throw new IllegalStateException("This Attribute has no associated Production, please use hasProduction() before you call getAttribute().");
        return attribute;
    }
}
